/**
 * 
 */
package due.pc.pcshoot.gamelogic;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.scene.Scene;

import due.pc.pcshoot.control.Objektiv;
import due.pc.pcshoot.control.Objektiv.IObjectiveLogicHandler;
import due.pc.pcshoot.gamelogic.BallLogicHandler.FiredBallModifier;
import due.pc.pcshoot.sensor.MotionDetector2;

/**
 * @author manhhung.vu
 *
 */
public class ObjectiveLogicHandler
{
	//===============================================================/
	//Constants
	//===============================================================/
	
	
	//===============================================================/
	//Fields
	//===============================================================/
	private Objektiv mObjektiv;
	private Scene mScene;
	
	private PhysicsHandler mObjektivPhysicsHandler;
	
	//===============================================================/
	//Constructors
	//===============================================================/
	public ObjectiveLogicHandler(Objektiv pObjektiv, Scene pScene, MotionDetector2 pDetector)
	{
		mObjektiv = pObjektiv;
		mScene = pScene;
		
		mScene.attachChild(mObjektiv);
		
		mObjektivPhysicsHandler = new PhysicsHandler(mObjektiv);
		mObjektiv.registerUpdateHandler(mObjektivPhysicsHandler);
		
		SensorDataHandler sensorHandler = new SensorDataHandler(pDetector, mObjektivPhysicsHandler);
		mObjektiv.setLogicHandler(sensorHandler);
		
		mScene.registerUpdateHandler(sensorHandler);
	}
	
	//===============================================================/
	//Inherited Methods
	//===============================================================/
	
	
	//===============================================================/
	//Methods
	//===============================================================/
	
	
	//===============================================================/
	//Getters & Setters
	//===============================================================/
	
	
	//===============================================================/
	//Static Methods
	//===============================================================/
	
	
	//===============================================================/
	//Inner Classes
	//===============================================================/
	class SensorDataHandler implements IUpdateHandler, IObjectiveLogicHandler
	{
		//===============================================================/
		//Constants
		//===============================================================/
		//Spiel mit den Faktoren um die beste Geschwindigkeit festzustellen
		static final float ACCELEROMETER_TO_VELOCITY_FACTOR = 1;
		static final float ORIENTATION_TO_VELOCITY_FACTOR_X = 1.5f;
		static final float ORIENTATION_TO_VELOCITY_FACTOR_Y = 1.0f;
		static final float FRICTION_FACTOR = 0.85f;
		
		
		//===============================================================/
		//Fields
		//===============================================================/
		private MotionDetector2 mDetector;
		private PhysicsHandler mPhysicsHandler;
		
		private float mLastVx = 0;
		private float mLastVy = 0;
		
		//===============================================================/
		//Constructors
		//===============================================================/
		public SensorDataHandler(MotionDetector2 pDetector, PhysicsHandler pPhysicsHandler) 
		{
			mDetector = pDetector;
			mPhysicsHandler = pPhysicsHandler;
		}
		
		//===============================================================/
		//Inherited Methods
		//===============================================================/
		@Override
		public void onUpdate(float pSecondsElapsed) 
		{
			mDetector.prepareSensorData();
			
			float gx = 0, gy = 0, ox = 0, oy = 0;
			
			gx = mDetector.getLeftRightAcceleration();
			gy = mDetector.getTopBottomAcceleration();
			
//			ox = mDetector.getLeftRightDeltaAngel();
//			oy = mDetector.getTopBottomDeltaAngel();
//					
			ox = mDetector.getLeftRightAngel();
			oy = mDetector.getTopBottomAngel();
			
			float x =  mLastVx * (1 - FRICTION_FACTOR * pSecondsElapsed) +
						gx * pSecondsElapsed * ACCELEROMETER_TO_VELOCITY_FACTOR + 
						ox * ORIENTATION_TO_VELOCITY_FACTOR_X;
			float y =  mLastVy + (1 - FRICTION_FACTOR * pSecondsElapsed) +
						gy * pSecondsElapsed * ACCELEROMETER_TO_VELOCITY_FACTOR + 
						oy * ORIENTATION_TO_VELOCITY_FACTOR_Y;

			mLastVx = x;
			mLastVy = y;

//			mPhysicsHandler.setVelocity(x , y);
			float t = (pSecondsElapsed == 0) ? 0 : 0.03f;
			
			mObjektiv.setPosition(mObjektiv.getX() + x * t, mObjektiv.getY() + y * t);
		}
		
		@Override
		public void reset() {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onOutOfBottom() {
			mLastVy = 0;
		}
		
		@Override
		public void onOutOfLeft() {
			mLastVx = 0;
		}
		
		@Override
		public void onOutOfRight() {
			mLastVx = 0;
		}
		
		@Override
		public void onOutOfTop() {
			mLastVy = 0;
		}
	}
}
